package me.shingohu.man.di.module;

import android.app.Application;

import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import me.shingohu.man.di.config.HttpClientConfig;
import me.shingohu.man.intergation.IRepositoryManager;
import me.shingohu.man.intergation.RepositoryManager;
import me.shingohu.man.intergation.retrofit.NobodyConverterFactory;
import me.shingohu.man.net.GlobeHttpHandler;
import me.shingohu.man.net.RequestInterceptor;
import me.shingohu.man.net.cookie.OkHttpCookieJar;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by hujie on 17/4/14.
 */

@Module
public class ClientModule {

    @Singleton
    @Provides
    Retrofit provideRetrofit(Retrofit.Builder builder, final OkHttpClient okHttpClient, HttpUrl httpUrl) {
        return builder.baseUrl(httpUrl)
                .client(okHttpClient)
                .addConverterFactory(NobodyConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Singleton
    @Provides
    OkHttpClient provideHttpClient(OkHttpClient.Builder builder, final HttpClientConfig config, GlobeHttpHandler handler) {

        builder.writeTimeout(config.getReadTimeout(), TimeUnit.SECONDS)
                .connectTimeout(config.getConnectTimeout(), TimeUnit.SECONDS)
                .readTimeout(config.getReadTimeout(), TimeUnit.SECONDS)
                .retryOnConnectionFailure(config.isRetryOnConnectionFailure())
                .cookieJar(config.getCookieJar())
                .addInterceptor(chain -> chain.proceed(handler.onHttpRequestBefore(chain, chain.request())))
                .addInterceptor(config.getInterceptor());
        return builder.build();
    }


    @Singleton
    @Provides
    Retrofit.Builder provideRetrofitBuilder() {
        return new Retrofit.Builder();
    }


    @Singleton
    @Provides
    OkHttpClient.Builder provideClientBuilder() {
        return new OkHttpClient.Builder();
    }



    @Singleton
    @Provides
    Interceptor provideInterceptor(RequestInterceptor intercept) {
        return intercept;//打印请求信息的拦截器
    }


    @Singleton
    @Provides
    Gson provideGson() {
        return new Gson();
    }



    @Singleton
    @Provides
    CookieJar providerCoolkieJar(Application application) {
        return new OkHttpCookieJar(application);
    }


    @Singleton
    @Provides
    HttpClientConfig provideHttpClientConfig(CookieJar cookieJar, Interceptor interceptor) {
        HttpClientConfig config = new HttpClientConfig();
        config.setConnectTimeout(3 * 60);
        config.setReadTimeout(3 * 60);
        config.setWriteTimeout(5 * 60);
        config.setRetryOnConnectionFailure(true);
        config.setCookieJar(cookieJar);
        config.setInterceptor(interceptor);
        return config;
    }

    @Singleton
    @Provides
    public IRepositoryManager provideRepositoryManager(RepositoryManager repositoryManager) {
        return repositoryManager;
    }


}
